package com.net.xpay.common.manager.impl.notify;

import com.google.common.base.Preconditions;
import com.net.common.dao.BasicMapper;
import com.net.common.dao.Page;
import com.net.xpay.common.dao.notify.SafePayBatchNotifyRecordMapper;
import com.net.xpay.common.domain.notify.SafePayBatchNotifyRecord;
import com.net.xpay.common.manager.notify.SafePayBatchNotifyRecordManager;
import com.net.common.manager.AbstractManager;
import com.net.xpay.common.model.search.SafePayBatchNotifyRecordSearchModel;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@Component
public class SafePayBatchNotifyRecordManagerImpl extends AbstractManager<SafePayBatchNotifyRecord> implements SafePayBatchNotifyRecordManager{

    @Autowired
    private SafePayBatchNotifyRecordMapper mapper;


    @Override
    protected BasicMapper<Long, SafePayBatchNotifyRecord> getMapper() {
        return mapper;
    }


    @Override
    public List<SafePayBatchNotifyRecord> search(SafePayBatchNotifyRecordSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        Page page = new Page(searchModel);
        page.addOrder("id", "desc");
        return nonEmptyList(mapper.search(searchModel, page));
    }

    @Override
    public long searchCount(SafePayBatchNotifyRecordSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return mapper.searchCount(searchModel);
    }

    @Override
    public int deleteFinishedRecord(LocalDateTime deadline) {
        Preconditions.checkArgument(deadline != null);
        return mapper.deleteFinishedRecord(deadline);
    }

    @Override
    public List<Long> queryWaitNotifyRecord(long limit, LocalDateTime maxNextNotifyTime) {
        List<Long> ids = mapper.queryWaitNotifyRecord(limit, maxNextNotifyTime);
        if (CollectionUtils.isEmpty(ids)) {
            return new ArrayList<>();
        }
        return ids;
    }

    @Override
    public SafePayBatchNotifyRecord getBySafePayBatchId(Long safePayBatchId) {
        Preconditions.checkArgument(safePayBatchId != null);
        SafePayBatchNotifyRecord condition = new SafePayBatchNotifyRecord();
        condition.setSafePayBatchId(safePayBatchId);
        return getByCondition(condition);
    }
}
